/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package music;

/**
 *
 * @author TJ
 */
import java.awt.Graphics;
import java.util.Iterator;
import java.util.Vector;
import javax.sound.midi.Sequence;
import javax.sound.midi.Track;
import music.types.*;
import structures.Voice;
public class Staff {
    private Score parent;
    private ClefType defaultClefType=ClefType.TREBLE;
    private Instrument instr;
    private int lines=200;
   
    private Vector<Measure> measures;
   /* public Staff(){
        init(1);
    }*/
    public Staff(Score p) {
        parent=p;
        init(1);
        
    }
    public Staff(Score p, int measures) {
        init(measures);
        parent=p;
    }
    public void setScore(Score s) {parent=s;}
    public int calculateHeight(){
        return lines+lines*parent.getSpacing().getLineSpacing();
    }
    public void setDefaultClef(ClefType c){defaultClefType=c;}
    public ClefType getDefaultClefType(){return defaultClefType;}
    public int getBottomLine() {return lines;}
    protected void setBottomLine(int l){lines=l;}
    public int countMeasures() {return measures.size();}
    protected Vector<Measure> getMeasures(){return measures;}
    protected Score getScore(){return parent;}
    public Measure getMeasure(int m) {return measures.get(m);}
    private void init(int m){
        measures=new Vector<Measure>();
        for(int i=0; i<m; i++){
            addMeasure();
        }
    }
    public void addMeasure(Measure m){
        m.setStaff(this);
        measures.add(m);
    }
    public int getMeasureNumber(Measure m){
        return measures.indexOf(m);
    }
    public TimeSignature getTimeAt(Measure m) {
        return getScore().getTimeAt(measures.indexOf(m));
    }
    public KeySignature getKeyAt(Measure m) {
        KeySignature key=getScore().getKeyAt(measures.indexOf(m));
//        if(instr!=null){return instr.keyTranspose(key);}
        return key;
    }
    public Measure addMeasure() {
        Measure m;
            m=new Measure(this);
        measures.add(m);
        return m;
    }
    protected void getTrack(Sequence t){
        Track t2=t.createTrack();
        for(Measure m : measures) {
            for(Voice v  : m.getNotes().getVoices()) {
                Iterator it=v.getIterator();
                while(it.hasNext()) {
                    
                }
            }
        }
        
    }
    protected int renderMeasures(Graphics g, int x) {
        int start=x;
        SpacingOptions opt=this.getScore().getSpacing();
        ClefType lastrendered=null;
        TimeSignature lasttime=null;
        int num=0;
        for(Measure m : this.getMeasures()) {
            num++;
            TimeSignature tmp=this.getTimeAt(m);
            boolean render=lasttime==null || !lasttime.equals(tmp);
            int wid=m.calculateWidth(opt,lastrendered,render);
            lastrendered=m.render(g,this.getScore().getSpacing(), x,lastrendered,render);
            x+=wid;
            lasttime=tmp;
            
        }
        return x;
    }
    public void render(Graphics g, int x) {
       int fin=renderMeasures(g,x);
      
       for(int i=0; i<5; i++) {
            int y=getBottomLine()-i*this.getScore().getSpacing().getLineSpacing();
           g.drawLine(x, y, fin, y);
       }
    }
}
